/**
 * 
 */
package com.ieasy.module.oa.project.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.model.Pager;
import com.ieasy.basic.util.BeanUtils;
import com.ieasy.basic.util.ClobUtil;
import com.ieasy.module.common.service.BaseService;
import com.ieasy.module.oa.project.entity.YHYLEntity;
import com.ieasy.module.oa.project.web.form.YHYLEntityForm;

/**
 * @author 杨浩泉
 *
 */
@Service @Transactional
public class YHYLService extends BaseService implements IYHYLService {
	
	@Inject
	private IBaseDao<YHYLEntity> basedaoYHYL ;
	
	@Override
	public Msg add(YHYLEntityForm form) {
		try {
			YHYLEntity entity = new YHYLEntity() ;
			BeanUtils.copyNotNullProperties(form, entity, new String[]{"remark"}) ;
			entity.setRemark(ClobUtil.getClob(form.getRemark())) ;
			entity.setCreated(new Date()) ;
			entity.setCreateName(this.getCurrentUser().getEmp_name()) ;
			
			this.basedaoYHYL.add(entity) ;
			
			return new Msg(true, "添加成功！");
		} catch (Throwable e) {
			return new Msg(false, "添加失败！") ;
		}
	}

	@Override
	public Msg delete(YHYLEntityForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					this.basedaoYHYL.delete(YHYLEntity.class, id) ;
				}
				return new Msg(true, "删除成功！") ;
			}
		} catch (Exception e) {
			return new Msg(false, "删除失败！") ;
		}
		return null;
	}

	@Override
	public Msg update(YHYLEntityForm form) {
		try {
			YHYLEntity entity = this.basedaoYHYL.load(YHYLEntity.class, form.getId()) ;
			BeanUtils.copyNotNullProperties(form, entity, new String[]{"remark"}) ;
			entity.setRemark(ClobUtil.getClob(form.getRemark())) ;
			entity.setModifyDate(new Date()) ;
			entity.setModifyName(this.getCurrentUser().getEmp_name()) ;
			
			this.basedaoYHYL.update(entity) ;
			return new Msg(true, "修改成功！");
		} catch (Exception e) {
			return new Msg(false, "修改信息失败！") ;
		}
	}
	
	@Override
	public YHYLEntityForm get(YHYLEntityForm form) {
		try {
			YHYLEntity entity = this.basedaoYHYL.load(YHYLEntity.class, form.getId()) ;
			BeanUtils.copyNotNullProperties(entity, form, new String[]{"remark"}) ;
			form.setRemark(ClobUtil.getString(entity.getRemark())) ;
			
			return form ;
		} catch (Exception e) {
		}
		return null ;
	}

	@Override
	public DataGrid datagrid(YHYLEntityForm form) {
		try {
			List<YHYLEntityForm> forms = new ArrayList<YHYLEntityForm>() ;
			Pager<YHYLEntityForm> pager = this.find(form) ;
			if (null != pager && !pager.getDataRows().isEmpty()) {
				for(YHYLEntityForm pf : pager.getDataRows()) {
					forms.add(pf) ;
				}
			}
			DataGrid dg = new DataGrid() ;
			dg.setTotal(pager.getTotal());
			dg.setRows(forms);
			return dg;
		} catch (Exception e) {
		}
		return null;
	}
	
	private Pager<YHYLEntityForm> find(YHYLEntityForm form) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String sql = ""
				+ "select "
				+ "t.*, "
				+ "o.id as org_id, o.name as org_name "
				+ "from ieasy_oa_yhyl t "
				+ "left join ieasy_sys_org o ON(t.zrbmId=o.id) " 
				+ "where 1=1 ";
		sql = addWhere(sql, form, alias) ;
		
		return this.basedaoYHYL.findSQL(sql, alias, YHYLEntityForm.class, false) ;
	}
	
	private String addWhere(String sql, YHYLEntityForm form, Map<String, Object> params) {
		if (null != form) {
			if (form.getYhId() != null && !"".equals(form.getYhId().trim())) {
				sql += " and t.yhId like :yhId";
				params.put("yhId", "%%" + form.getYhId() + "%%");
			}
			if (form.getAjmc() != null && !"".equals(form.getAjmc().trim())) {
				sql += " and t.ajmc like :ajmc";
				params.put("ajmc", "%%" + form.getAjmc() + "%%");
			}
			if (form.getProjectId() != null && !"".equals(form.getProjectId().trim())) {
				sql += " and t.projectId like :projectId";
				params.put("projectId", "%%" + form.getProjectId() + "%%");
			}
			if (form.getOrg_id() != null && !"".equals(form.getOrg_id().trim())) {
				sql += " and t.zrbmId=:zrbmId";
				params.put("zrbmId", form.getOrg_id());
			}
			if (form.getAjzl() != null && !"".equals(form.getAjzl().trim())) {
				sql += " and t.ajzl=:ajzl";
				params.put("ajzl", form.getAjzl());
			}
			if (form.getSzzk() != null && !"".equals(form.getSzzk().trim())) {
				sql += " and t.szzk=:szzk";
				params.put("szzk", form.getSzzk());
			}
			if (form.getClient() != null && !"".equals(form.getClient().trim())) {
				sql += " and t.client=:client";
				params.put("client", form.getClient());
			}
			if (form.getHtpszk() != null && !"".equals(form.getHtpszk().trim())) {
				sql += " and t.htpszk=:htpszk";
				params.put("htpszk", form.getHtpszk());
			}
			if (form.getKsStartDate() != null && !"".equals(form.getKsStartDate())) {
				sql += " and date_format(t.ksyd,'%Y-%m-%d')>= date_format(:startDate,'%Y-%m-%d') ";
				params.put("startDate", form.getKsStartDate());
			}
			if (form.getKsEndDate() != null && !"".equals(form.getKsEndDate())) {
				sql += " and date_format(t.ksyd,'%Y-%m-%d')<= date_format(:endDate,'%Y-%m-%d') ";
				params.put("endDate", form.getKsEndDate());
			}
			if (form.getJsStartDate() != null && !"".equals(form.getJsStartDate())) {
				sql += " and date_format(t.jsyd,'%Y-%m-%d')>= date_format(:startDate,'%Y-%m-%d') ";
				params.put("startDate", form.getJsStartDate());
			}
			if (form.getJsEndDate() != null && !"".equals(form.getJsEndDate())) {
				sql += " and date_format(t.jsyd,'%Y-%m-%d')<= date_format(:endDate,'%Y-%m-%d') ";
				params.put("endDate", form.getJsEndDate());
			}
			sql += " order by t.projectId,t.yhId" ;
			
		}
		return sql;
	}

	@Override
	public Msg updateProjectId(YHYLEntityForm form) {
		try {
			
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				if(ids.length>0) {
					Map<String, Object> alias = new HashMap<String, Object>() ;
					alias.put("projectId", form.getProjectId()) ;
					alias.put("ids", ids) ;
					String sql = "update ieasy_oa_yhyl t SET t.projectId=:projectId WHERE t.id IN(:ids)" ;
					this.basedaoYHYL.executeSQL(sql, alias) ;
					return new Msg(true, "设置项目ID成功！");
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return new Msg(false, "设置项目ID失败！") ;
		}
		return new Msg(false, "设置项目ID失败！") ;
	}
	

}
